<?php

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/mblog/inc.php");

$g_cgival = array();
$g_show = array();
$g_pro = array();

if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	cgi::both($g_cgival['mblog'],"mblog","");
	$g_cgival['mblog'] = trim($g_cgival['mblog']);
	$g_cgival['mblog'] = string::un_html($g_cgival['mblog']);
	
	cgi::both($g_cgival['id'],"id",0);
	
	cgi::both($g_cgival['type'],"type","");
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	
	global $g_cgival, $g_show, $g_pro;
	
	$mblog = trim($g_cgival['mblog']);
	$id = $g_cgival['id'];
	
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		
		
	}
	else
	{
		
		throw new Exception("未登录");
	}	
	
	$db_mblog_user_rt = new db_mblog_user_rt();
	$db_mblog_rt_mid = new db_mblog_rt_mid();
	
	
	if($g_cgival['type'] == "del")
	{
		
		$item = $db_mblog_user_rt->get_item_by_id($id);
		$mid = $item["mid"];
		$have_mid_count = $db_mblog_user_rt->get_user_rt_count_by_mid($mid);
		$res  = $db_mblog_user_rt->update_mblog_user_rt_by_id($id,0);
		
		
		if($have_mid_count <= 1)
		{
			$db_mblog_rt_mid->update_by_mid($mid,0);
		}
		
		
		
	}
	else
	{
		preg_match_all("/\/.+?\/.+?\/([A-Za-z0-9]*)/",$mblog,$match);
		$mid = $match[1][0];
		$mid = Base62Tool::decode($mid);
		
		$mblog_from_api = new mblog_from_api();
		$res = $mblog_from_api->api_get_mblog_by_mids($mid);
		
		if(!$res["data"]["result"]["{$mid}"])
		{
			throw new Exception("没有抓到这条微博！");
		}
		if($res["data"]["result"]["{$mid}"]["retweeted_status"])
		{
			throw new Exception("输入的微博必须是原创！");
		}
		
		
		$res  = $db_mblog_user_rt->insert_mblog_user_rt($uid,$mid,$mblog);
		$res = $db_mblog_rt_mid->insert_mblog_rt_mid($mid,time()+86400*7);

	}
	
	if(!$res)
	{
		throw new Exception("数据库操作失败");
	}
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "操作成功";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}